/**
 * -requires OpenLayers/Layer/Grid.js
 * -requires OpenLayers/Tile/Image.js
 */
 
/** 
 * Class: OpenLayers.Layer.WeWXTiles
 * 
 * Inherits from:
 *  - <OpenLayers.Layer.TMS>
 */
OpenLayers.Layer.WeWXTiles = OpenLayers.Class(OpenLayers.Layer.TMS, {
    /**
     * APIProperty: isBaseLayer
     * Default is true, as this is designed to be a base tile source. 
     */
    isBaseLayer: false,
    
    // hide by default
    visibility: false,
    
    wrapDateLine: true,
    
    numZoomLevels: 6,
    
    // legend window
    legend: null,
    
    // legend image
    legendImage: null,
    
    // current UTC time string
    strUTC: null,
    
    forecastHours: 0,
    
    // reference to the OS
    os: null,
    
    /**
     * Constructor: OpenLayers.Layer.WeWXTiles
     * 
     * Parameters:
     * name - {String} Title to be displayed in a <OpenLayers.Control.LayerSwitcher>
     * url - {String} Service endpoint (without the version number).  E.g.
     *     "http://tms.osgeo.org/".
     * options - {Object} Additional properties to be set on the layer.  The
     *     <layername> and <type> properties must be set here.
     */
    initialize: function(name, url, options) {
        OpenLayers.Layer.TMS.prototype.initialize.apply(this, arguments);
        // set private properties here
        this.attribution = "Weather &copy; <a href=http://www.wxtiles.com/ target=_blank>WXTiles</a>";
        this.strUTC = this.getUTCTime();
        // register events
        this.events.register('visibilitychanged', this, this.onVisibilityChanged);
        
        this.os = (options && options.os) ? options.os : null;
    },
    
    /**
     * APIMethod:destroy
     */
    destroy: function() {
        // close the legend window if opened
        if (this.legend) {
            this.legend.close();
            this.legend = null;
        }
        
        this.os = null;
        
        OpenLayers.Layer.TMS.prototype.destroy.apply(this, arguments);  
    },
    
    /**
     * APIMethod: clone
     * Create a clone of this layer
     *
     * Parameters:
     * obj - {Object} Is this ever used?
     * 
     * Returns:
     * {<OpenLayers.Layer.WeWXTiles>} An exact clone of this OpenLayers.Layer.WeWXTiles
     */
    clone: function (obj) {
        if (obj == null) {
            obj = new OpenLayers.Layer.WeWXTiles(this.name, this.url, this.getOptions());
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.TMS.prototype.clone.apply(this, [obj]);

        return obj;
    },
    
    setOS: function(os) {
        if (!this.os) {
            this.os = os;
        }
    },

    /**
     * Method: getURL
     *
     * Parameters:
     * d - {<OpenLayers.Bounds>}
     *
     * Returns:
     * {String} A string with the layer's url and parameters and also the
     *          passed-in bounds and appropriate tile size specified as
     *          parameters
     */
    getURL: function(d) {
        var c = this.map.getResolution();
        var e = this.map.getZoom();
        var a = Math.round((d.left - this.tileOrigin.lon) / (c * this.tileSize.w));
        var b = Math.pow(2, e);
        a = (a % b + b) % b;
        var f = Math.round((d.bottom - this.tileOrigin.lat) / (c * this.tileSize.h));
        return this.url + this.strUTC + "/" + e + "/" + a + "/" + f + '.png';    
    },
    
    // get string of current time
    getUTCTime: function() {
        var dt, yy, mm, dd, hh;
        dt = new Date();
        dt.setTime(dt.getTime() + this.forecastHours*3600000); // x hours forecast
        yy = dt.getUTCFullYear();
        mm = dt.getUTCMonth() + 1;
        dd = dt.getUTCDate();
        hh = dt.getUTCHours();
        hh = hh - (hh%6);
        return '' + yy + this.zer0(mm) + this.zer0(dd) + '_' + this.zer0(hh) + 'z';
    },
     
    zer0: function (dd) {
        // 1 => 01
        if (String(dd).length < 2) {
            dd = '0' + dd;
        }
        return dd;
    },
    
    onVisibilityChanged: function(evt) {
        if (this.getVisibility()) {
            // open legend window
            if (this.legendImage != null && this.os != null) {
                this.legend = this.os.getWindowManager().createWindow({
                    title: (this.name + ': UTC ' + this.strUTC),
                    width: 375,
                    height: 100,
                    alwaysOnTop: true,
                    iconImageURL: 'http://www.wxtiles.com/images/wxtiles_48.png'
                });
                this.legend.setContent(
                    "<img src=" + this.url + this.legendImage + " />" +
                    "<a href=http://www.wxtiles.com/ target=_blank><img src=http://www.wxtiles.com/images/wxtiles_48.png /></a>"
                );
            }
        } else {
            // close legend window
            if (this.legend != null) {
                this.legend.close();
                this.legend = null;
            }
        }
    },

    CLASS_NAME: "OpenLayers.Layer.WeWXTiles"
});

// global rain
OpenLayers.Layer.WeWXRain = OpenLayers.Class(OpenLayers.Layer.WeWXTiles, {
    forecastHours: 6,
    
    initialize: function(name, url, options) {
        OpenLayers.Layer.WeWXTiles.prototype.initialize.apply(this, arguments);
        this.name = name ? name : OpenLayers.i18n('WeWXRain_name');
        this.url = url ? url : 'http://tiles.wxtiles.com/rain/';
        this.legendImage = 'rain_small_horiz.png';
    },
    
    clone: function (obj) {
        if (obj == null) {
            obj = new OpenLayers.Layer.WeWXRain(this.name, this.url, this.getOptions());
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.WeWXTiles.prototype.clone.apply(this, [obj]);

        return obj;
    },    
    
    CLASS_NAME: "OpenLayers.Layer.WeWXRain"
});

// global wind
OpenLayers.Layer.WeWXWind = OpenLayers.Class(OpenLayers.Layer.WeWXTiles, {
    forecastHours: 6,

    initialize: function(name, url, options) {
        OpenLayers.Layer.WeWXTiles.prototype.initialize.apply(this, arguments);
        this.name = name ? name : OpenLayers.i18n('WeWXWind_name');
        this.url = url ? url : 'http://tiles.wxtiles.com/wind/';
        this.legendImage = 'wind_small_horiz.png';
    },
    
    clone: function (obj) {
        if (obj == null) {
            obj = new OpenLayers.Layer.WeWXWind(this.name, this.url, this.getOptions());
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.WeWXTiles.prototype.clone.apply(this, [obj]);

        return obj;
    },    
    
    CLASS_NAME: "OpenLayers.Layer.WeWXWind"
});

// global air temperature
OpenLayers.Layer.WeWXTempAir = OpenLayers.Class(OpenLayers.Layer.WeWXTiles, {
    initialize: function(name, url, options) {
        OpenLayers.Layer.WeWXTiles.prototype.initialize.apply(this, arguments);
        this.name = name ? name : OpenLayers.i18n('WeWXTempAir_name');
        this.url = url ? url : 'http://tiles.wxtiles.com/tmp/';
        this.legendImage = 'tmp_small_horiz.png';
    },
    
    clone: function (obj) {
        if (obj == null) {
            obj = new OpenLayers.Layer.WeWXTempAir(this.name, this.url, this.getOptions());
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.WeWXTiles.prototype.clone.apply(this, [obj]);

        return obj;
    },    
    
    CLASS_NAME: "OpenLayers.Layer.WeWXTempAir"
});

// global sea temperature
OpenLayers.Layer.WeWXTempSea = OpenLayers.Class(OpenLayers.Layer.WeWXTiles, {
    initialize: function(name, url, options) {
        OpenLayers.Layer.WeWXTiles.prototype.initialize.apply(this, arguments);
        this.name = name ? name : OpenLayers.i18n('WeWXTempSea_name');
        this.url = url ? url : 'http://tiles.wxtiles.com/sst/';
        this.legendImage = 'sst_small_horiz.png';
    },
    
    clone: function (obj) {
        if (obj == null) {
            obj = new OpenLayers.Layer.WeWXTempSea(this.name, this.url, this.getOptions());
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.WeWXTiles.prototype.clone.apply(this, [obj]);

        return obj;
    },    
    
    CLASS_NAME: "OpenLayers.Layer.WeWXTempSea"
});

// global wave height
OpenLayers.Layer.WeWXSwellHeight = OpenLayers.Class(OpenLayers.Layer.WeWXTiles, {
    initialize: function(name, url, options) {
        OpenLayers.Layer.WeWXTiles.prototype.initialize.apply(this, arguments);
        this.name = name ? name : OpenLayers.i18n('WeWXSwellHeight_name');
        this.url = url ? url : 'http://tiles.wxtiles.com/hs/';
        this.legendImage = 'hs_small_horiz.png';
    },
    
    clone: function (obj) {
        if (obj == null) {
            obj = new OpenLayers.Layer.WeWXSwellHeight(this.name, this.url, this.getOptions());
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.WeWXTiles.prototype.clone.apply(this, [obj]);

        return obj;
    },    
    
    CLASS_NAME: "OpenLayers.Layer.WeWXSwellHeight"
});

// global swell period
OpenLayers.Layer.WeWXSwellPeriod = OpenLayers.Class(OpenLayers.Layer.WeWXTiles, {
    initialize: function(name, url, options) {
        OpenLayers.Layer.WeWXTiles.prototype.initialize.apply(this, arguments);
        this.name = name ? name : OpenLayers.i18n('WeWXSwellPeriod_name');
        this.url = url ? url : 'http://tiles.wxtiles.com/tp/';
        this.legendImage = 'tp_small_horiz.png';
    },
    
    clone: function (obj) {
        if (obj == null) {
            obj = new OpenLayers.Layer.WeWXSwellPeriod(this.name, this.url, this.getOptions());
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.WeWXTiles.prototype.clone.apply(this, [obj]);

        return obj;
    },    
    
    CLASS_NAME: "OpenLayers.Layer.WeWXSwellPeriod"
});

// global IR cloud
OpenLayers.Layer.WeWXCloudIR = OpenLayers.Class(OpenLayers.Layer.WeWXTiles, {
    initialize: function(name, url, options) {
        OpenLayers.Layer.WeWXTiles.prototype.initialize.apply(this, arguments);
        this.name = name ? name : OpenLayers.i18n('WeWXCloudIR_name');
        this.url = url ? url : 'http://74.208.230.214/sat/satir/';
    },
    
    clone: function (obj) {
        if (obj == null) {
            obj = new OpenLayers.Layer.WeWXCloudIR(this.name, this.url, this.getOptions());
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.WeWXTiles.prototype.clone.apply(this, [obj]);

        return obj;
    },
    
    CLASS_NAME: "OpenLayers.Layer.WeWXCloudIR"
});

// global visible cloud
OpenLayers.Layer.WeWXCloudVisible = OpenLayers.Class(OpenLayers.Layer.WeWXTiles, {
    initialize: function(name, url, options) {
        OpenLayers.Layer.WeWXTiles.prototype.initialize.apply(this, arguments);
        this.name = name ? name : OpenLayers.i18n('WeWXCloudVisible_name');
        this.url = url ? url : 'http://74.208.230.214/sat/satvis/';
    },
    
    clone: function (obj) {
        if (obj == null) {
            obj = new OpenLayers.Layer.WeWXCloudVisible(this.name, this.url, this.getOptions());
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.WeWXTiles.prototype.clone.apply(this, [obj]);

        return obj;
    },  
    
    CLASS_NAME: "OpenLayers.Layer.WeWXCloudVisible"
});

// global color enhanced cloud
OpenLayers.Layer.WeWXCloudColored = OpenLayers.Class(OpenLayers.Layer.WeWXTiles, {
    initialize: function(name, url, options) {
        OpenLayers.Layer.WeWXTiles.prototype.initialize.apply(this, arguments);
        this.name = name ? name : OpenLayers.i18n('WeWXCloudColored_name');
        this.url = url ? url : 'http://74.208.230.214/sat/satenh/';
    },
    
    clone: function (obj) {
        if (obj == null) {
            obj = new OpenLayers.Layer.WeWXCloudColored(this.name, this.url, this.getOptions());
        }

        //get all additions from superclasses
        obj = OpenLayers.Layer.WeWXTiles.prototype.clone.apply(this, [obj]);

        return obj;
    },
    
    CLASS_NAME: "OpenLayers.Layer.WeWXCloudColored"
});
